﻿$(document).ready(function () {
    $("#btVerifyCode").bind("click",function () {
        GetVerifyCode();
    });

    $("#reg").bind("click", function () {
        RegisterAccount();
    });

    GetInviterName();
});

function GetInviterName() {
    var postData = {
        Member_id: $("#inviteId").val()
    }

    $.ajax({
        url: "/api/AccountApi/GetInviterName",
        type: "POST",
        data: postData,
        success: function (data) {
            var result = JSON.parse(data);
            if (result == null || result.Ok == false) {
            }
            else {
                $("#inviterName").html("推荐人:" + result.result);
            }
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
        }
    });
}

function GetVerifyCode() {
    if ($("#btVerifyCode").attr('rel') == 'click') {
        var postData = {
            Phone: $("#mobile").val()
        }

        if ($("#mobile").val().length < 11) {
            alert("手机号码不正确");
            return;
        }

        $.ajax({
            url: "/api/AccountApi/GetVerifyCode",
            type: "POST",
            data: postData,
            success: function (data) {
                var result = JSON.parse(data);
                if (result.Ok == false) {
                    if (result.Message != null) {
                        alert(result.Message);
                    }
                }
                else {
                    DecrTime(null);
                }
            },
            error: function (xmlHttpRequest, textStatus, errorThrown) {
            }
        });
    }
}

function DecrTime(sec)
{
    if(sec == null)
    {
        sec = 60;
    }
    if(sec > 0)
    {
        sec--;
        $("#btVerifyCode").attr('rel', 'nonclick');
        $("#btVerifyCode").css("color", "#999999");
        $("#showTime").html(sec + "秒");
        window.setTimeout(function () { DecrTime(sec) }, 1000);
    }
    else {
        $("#btVerifyCode").attr('rel', 'click');
        $("#btVerifyCode").css("color", "#0099cc");
        $("#btVerifyCode").text("重新获取");

        $("#showTime").html("60秒");
        
    }

}

function SetShowTime(num, t) {
    if (num == t) {
        $("#btVerifyCode").attr('rel', 'click');
        $("#showTime").html("60秒");
    } else {
        var showTime = t - num;
        $("#btVerifyCode").attr('rel','nonclick');
        $("#showTime").html(showTime + "秒");
    }
}

function RegisterAccount() {
    var postData = {
        Phone: $("#mobile").val(),
        Verify_code: $("#verifyCode").val(),
        Password: $("#pwd").val(),
        Inviter: $("#inviteId").val()
    }
    if ($("#pwd").val() != $("#rePwd").val() || $("#pwd").val().length < 6) {
        alert("2次密码输入不一致或密码少于6位");
        return;
    }

    $.ajax({
        url: "/api/AccountApi/RegisterAccount",
        type: "POST",
        data: postData,
        success: function (data) {
            var result = JSON.parse(data);
            if (result.Ok == false) {
                alert(result.Message);
            }
            else {
                alert("注册成功");
                location.href = "/account/invitesuccess";
            }
        },
        error: function (xmlHttpRequest, textStatus, errorThrown) {
            alert("status:" + xmlHttpRequest.status + ",textStatus:" + textStatus);
         
        }
    });
}